import { useCallback } from 'react';
import { useAppSelector, useAppDispatch } from './redux';
import { 
  setIsPlayerTurn, 
  updatePlayerActions
} from '../store/gameSlice';
import { useSocket } from './useSocket';
import { GamePhase } from '../store/gameSlice';

// 自定义Hook：处理游戏逻辑
export const useGame = () => {
  const dispatch = useAppDispatch();
  const { 
    currentGame, 
    isPlayerTurn, 
    playerActions 
  } = useAppSelector(state => state.game);
  const auth = useAppSelector(state => state.auth);
  const socket = useSocket();

  // 检查当前玩家是否在游戏中
  const isPlayerInGame = useCallback(() => {
    if (!currentGame || !auth.user) return false;
    return currentGame.players.some(player => player.id === auth.user!.id);
  }, [currentGame, auth.user]);

  // 获取当前玩家信息
  const getCurrentPlayer = useCallback(() => {
    if (!currentGame || !auth.user) return null;
    return currentGame.players.find(player => player.id === auth.user!.id) || null;
  }, [currentGame, auth.user]);

  // 执行游戏动作
  const makeAction = useCallback((action: 'check' | 'call' | 'raise' | 'fold' | 'all-in', amount?: number) => {
    if (!currentGame || !isPlayerTurn) return;
    
    // 通过Socket发送动作
    socket.makeAction(currentGame.id, action, amount);
    
    // 更新本地状态
    dispatch(setIsPlayerTurn(false));
    dispatch(updatePlayerActions({
      canCheck: false,
      canCall: false,
      canRaise: false,
      canFold: false,
      canAllIn: false,
    }));
  }, [currentGame, isPlayerTurn, socket, dispatch]);

  // 加入游戏
  const joinGame = useCallback((gameId: string) => {
    socket.joinGame(gameId);
  }, [socket]);

  // 离开游戏
  const leaveGame = useCallback((gameId: string) => {
    socket.leaveGame(gameId);
  }, [socket]);

  // 开始游戏
  const startGame = useCallback((gameId: string) => {
    socket.startGame(gameId);
  }, [socket]);

  // 检查是否可以执行某个动作
  const canPerformAction = useCallback((action: 'check' | 'call' | 'raise' | 'fold' | 'all-in') => {
    switch (action) {
      case 'check':
        return playerActions.canCheck;
      case 'call':
        return playerActions.canCall;
      case 'raise':
        return playerActions.canRaise;
      case 'fold':
        return playerActions.canFold;
      case 'all-in':
        return playerActions.canAllIn;
      default:
        return false;
    }
  }, [playerActions]);

  // 获取游戏阶段描述
  const getPhaseDescription = useCallback(() => {
    if (!currentGame) return '';
    
    switch (currentGame.phase) {
      case GamePhase.WAITING:
        return '等待玩家加入';
      case GamePhase.PREFLOP:
        return '翻牌前';
      case GamePhase.FLOP:
        return '翻牌';
      case GamePhase.TURN:
        return '转牌';
      case GamePhase.RIVER:
        return '河牌';
      case GamePhase.SHOWDOWN:
        return '摊牌';
      default:
        return '';
    }
  }, [currentGame]);

  // 计算需要跟注的金额
  const getCallAmount = useCallback(() => {
    if (!currentGame || !auth.user) return 0;
    
    const currentPlayer = getCurrentPlayer();
    if (!currentPlayer) return 0;
    
    return Math.max(0, currentGame.currentBet - currentPlayer.currentBet);
  }, [currentGame, auth.user, getCurrentPlayer]);

  return {
    currentGame,
    isPlayerInGame,
    getCurrentPlayer,
    isPlayerTurn,
    playerActions,
    makeAction,
    joinGame,
    leaveGame,
    startGame,
    canPerformAction,
    getPhaseDescription,
    getCallAmount,
  };
};